package com.promise.service;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


@Service
public class PrometheusService {

    //cpu使用率
    public String queryPrometheus(long start, long end, String step) throws IOException {
        // 构造Prometheus查询语句
        String query = "sum(rate(node_cpu_seconds_total{mode!=\"idle\"}[" + step + "])) by (instance,cpu)";

        // 构造Prometheus HTTP API的URL
        String prometheusUrl = "http://101.43.109.202:9090/api/v1/query_range?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) +
                "&start=" + start +
                "&end=" + end +
                "&step=" + step;

        // 创建HttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构造HTTP GET请求
            HttpGet httpGet = new HttpGet(prometheusUrl);

            // 发送HTTP请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                org.apache.http.HttpEntity entity = response.getEntity();

                // 将响应实体转换为字符串
                return EntityUtils.toString(entity);
            }
        }
    }

    //内存使用率
    public String queryMemoryUsage(long start, long end, String step) throws IOException {
        // 构造Prometheus查询语句
        String query = "((node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_Buffers_bytes - node_memory_Cached_bytes) / (node_memory_MemTotal_bytes)) * 100";

        // 构造Prometheus HTTP API的URL
        String prometheusUrl = "http://101.43.109.202:9090/api/v1/query_range?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) +
                "&start=" + start +
                "&end=" + end +
                "&step=" + step;

        // 创建HttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构造HTTP GET请求
            HttpGet httpGet = new HttpGet(prometheusUrl);

            // 发送HTTP请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                org.apache.http.HttpEntity entity = response.getEntity();

                // 将响应实体转换为字符串
                return EntityUtils.toString(entity);
            }
        }
    }

    //磁盘的使用率
    public String querydiskUsage(long start, long end, String step) throws IOException {
        // 构造Prometheus查询语句
        String query = "100 - (node_filesystem_free_bytes{fstype=~\"ext4|xfs\",mountpoint=\"/\"} / node_filesystem_size_bytes{fstype=~\"ext4|xfs\",mountpoint=\"/\"} *100)";

        // 构造Prometheus HTTP API的URL
        String prometheusUrl = "http://101.43.109.202:9090/api/v1/query_range?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) +
                "&start=" + start +
                "&end=" + end +
                "&step=" + step;

        // 创建HttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构造HTTP GET请求
            HttpGet httpGet = new HttpGet(prometheusUrl);

            // 发送HTTP请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                org.apache.http.HttpEntity entity = response.getEntity();

                // 将响应实体转换为字符串
                return EntityUtils.toString(entity);
            }
        }
    }

    //网络字节总数
    public String querynetworkUsage(long start, long end, String step) throws IOException {
        // 构造Prometheus查询语句
        String query = "sum by(instance) (irate(node_network_transmit_bytes_total{device!~\"bond.*?|lo\"}[5m]))";
        // 构造Prometheus HTTP API的URL
        String prometheusUrl = "http://101.43.109.202:9090/api/v1/query_range?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) +
                "&start=" + start +
                "&end=" + end +
                "&step=" + step;

        // 创建HttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构造HTTP GET请求
            HttpGet httpGet = new HttpGet(prometheusUrl);

            // 发送HTTP请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                org.apache.http.HttpEntity entity = response.getEntity();

                // 将响应实体转换为字符串
                return EntityUtils.toString(entity);
            }
        }
    }

    //tcp等待连接数
    public String querynetstatwaitUsage(long start, long end, String step) throws IOException {
        // 构造Prometheus查询语句
        String query = "count_netstat_wait_connections";
        // 构造Prometheus HTTP API的URL
        String prometheusUrl = "http://101.43.109.202:9090/api/v1/query_range?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) +
                "&start=" + start +
                "&end=" + end +
                "&step=" + step;

        // 创建HttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构造HTTP GET请求
            HttpGet httpGet = new HttpGet(prometheusUrl);

            // 发送HTTP请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                org.apache.http.HttpEntity entity = response.getEntity();

                // 将响应实体转换为字符串
                return EntityUtils.toString(entity);
            }
        }
    }

    //查询当前ping服务器是否丢包
    public String querylosspkUsage(long start, long end, String step) throws IOException {
        // 构造Prometheus查询语句
        String query = "(loss_pk)";
        // 构造Prometheus HTTP API的URL
        String prometheusUrl = "http://101.43.109.202:9090/api/v1/query_range?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) +
                "&start=" + start +
                "&end=" + end +
                "&step=" + step;

        // 创建HttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构造HTTP GET请求
            HttpGet httpGet = new HttpGet(prometheusUrl);

            // 发送HTTP请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                org.apache.http.HttpEntity entity = response.getEntity();

                // 将响应实体转换为字符串
                return EntityUtils.toString(entity);
            }
        }
    }

    //查询当前ping服务器延迟
    public String querytimepingUsage(long start, long end, String step) throws IOException {
        // 构造Prometheus查询语句
        String query = "(time_ping)";

        // 构造Prometheus HTTP API的URL
        String prometheusUrl = "http://101.43.109.202:9090/api/v1/query_range?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) +
                "&start=" + start +
                "&end=" + end +
                "&step=" + step;

        // 创建HttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构造HTTP GET请求
            HttpGet httpGet = new HttpGet(prometheusUrl);

            // 发送HTTP请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                org.apache.http.HttpEntity entity = response.getEntity();

                // 将响应实体转换为字符串
                return EntityUtils.toString(entity);
            }
        }
    }
}


